﻿package code {
	import flash.display.Sprite;
	import flash.ui.Keyboard;
	import flash.events.KeyboardEvent;
	import flash.utils.getTimer;
	import flash.events.*;
	
	// class acts as gameboard that sets everything up
	public class GameBoard extends Sprite {
		public var leftFlipper:Flipper;
		public var rightFlipper:Flipper;
		public var bumpers:Array;
		public var spinner:Spinner;
		public var walls:Array;
		public var spring:Spring;
		public var ball:Ball;
		public var collisionObjects:Array;
		public var previousTime:Number;
		public var boardTree:QuadTree;
		public const POLYGONS_TO_MAKE:int = 3;
		
		public const LEFT_FLIPPER_RETURN:Number = Math.PI / 6;
		public const RIGHT_FLIPPER_RETURN:Number = (5 * Math.PI) / 6
		
		public var totalTime:Number;
		public function GameBoard() {
			bumpers = new Array();
			walls = new Array();
			collisionObjects = new Array();
			previousTime = getTimer();
			totalTime = 0;
			init();
		}
		
		public function init(){
			makeBall();  // makes the balls
			makeFlippers();  // makes flippers
			makePolygonBumpers(); // makes polygon
			makeWalls();  // makes the walls
			makeSpring();  // makes the spring
			makeSpinner(); // makes the spinner
			populateCollisionObjects();  // populates collision objects
			makeQuadTree();
		}
		
		public function makeBall(){
			ball = new Ball("Polygon", 15, 50, 0x00FF00, 3, false);
			ball.x = 730;  // have to assign coordinates after creation or it fails
			ball.y = 700;
			ball.createLinesArray();
			addChild(ball);  // adds it to the stage
		}
		
		public function makeFlippers(){
			leftFlipper = new Flipper("Rectangle", 75, 4, 0xFF0000, 10, false, Math.PI / 6, 3, "left");
			leftFlipper.x = 385; // have to assign coordinates after creation or it fails
			leftFlipper.y = 600;
			leftFlipper.createLinesArray();
			addChild(leftFlipper);
			
			rightFlipper = new Flipper("Rectangle", 75, 4, 0xFF0000, 10, false, (5 * Math.PI) / 6, 3, "right");
			rightFlipper.x = 555;
			rightFlipper.y = 600;
			rightFlipper.createLinesArray();
			addChild(rightFlipper);
			

		}
		
		public function makePolygonBumpers(){
			var linesToPush:Array = new Array();
			for(var i:int = 0; i < POLYGONS_TO_MAKE; i++){  // makes three polygons
				var tempBumper = new Bumper("Polygon", 30, 5 + i, 0x00FF00, 3, false, Math.PI / 2);
				tempBumper.x = i * 150 + 300;
				tempBumper.y = 250;
				tempBumper.createLinesArray();
				bumpers.push(tempBumper);
				addChild(tempBumper);
			}
			
			var triangleBumper = new Bumper("Polygon", 50, 3, 0x00FF00, 3, false);
			triangleBumper.x = 410;
			triangleBumper.y = 400;
			triangleBumper.createLinesArray();
			bumpers.push(triangleBumper);
			addChild(triangleBumper);
		}
		
		public function makeWalls(){
			var leftBoundryWall:GameObject = new GameObject("Rectangle", 3, 4, 0x000000, 3, true, 0, 720);
			leftBoundryWall.x = 250;
			leftBoundryWall.y = 0;
			leftBoundryWall.createLinesArray();
			walls.push(leftBoundryWall);
			addChild(leftBoundryWall);
			
			var rightBoundryWall:GameObject = new GameObject("Rectangle", 3, 4, 0x000000, 3, true, 0, 720);
			rightBoundryWall.x = 750;
			rightBoundryWall.y = 0;
			rightBoundryWall.createLinesArray();
			walls.push(rightBoundryWall);
			addChild(rightBoundryWall);
			
			var bottomWall:GameObject = new GameObject("Rectangle", 700, 4, 0x000000, 3, true, 0, 5);
			bottomWall.x = 250;
			bottomWall.y = 720;
			bottomWall.createLinesArray();
			walls.push(bottomWall);
			addChild(bottomWall);
			
			var pinballLauncherWall:GameObject = new GameObject("Rectangle", 10, 4, 0x000000, 3, true, 0, 720);
			pinballLauncherWall.x = 680;
			pinballLauncherWall.y = 200;
			pinballLauncherWall.createLinesArray();
			walls.push(pinballLauncherWall);
			addChild(pinballLauncherWall);
			
			var topWall:GameObject = new GameObject("Rectangle", 500, 4, 0x000000, 5, true, 0, 50);
			topWall.x = 250;
			topWall.y = 0;
			topWall.createLinesArray();
			walls.push(topWall);
			addChild(topWall);
			
			var topBoundryWall:GameObject = new GameObject("Triangle", 5, 3, 0x000000, 1, true, 0, 100, new Vertex(-50, 0));
			topBoundryWall.x = 750;
			topBoundryWall.y = 50;
			topBoundryWall.createLinesArray();
			walls.push(topBoundryWall);
			addChild(topBoundryWall);
			
			var leftFlipperWall:GameObject = new GameObject("Rectangle", 10, 4, 0x000000, 3, true,  (5 * Math.PI / 3), 100);
			leftFlipperWall.x = 290;
			leftFlipperWall.y = 560;
			leftFlipperWall.createLinesArray();
			walls.push(leftFlipperWall);
			addChild(leftFlipperWall);
			
			var rightFlipperWall:GameObject = new GameObject("Rectangle", 10, 4, 0x000000, 3, true, Math.PI / 3, 100);
			rightFlipperWall.x = 640;
			rightFlipperWall.y = 550;
			rightFlipperWall.createLinesArray();
			walls.push(rightFlipperWall);
			addChild(rightFlipperWall);

		}
		
		public function makeSpring(){
			var tempSpring:Spring = new Spring("Rectangle", 3, 4, 0x000000, 3, false, 0, 100);
			tempSpring.x = 725;
			tempSpring.y = 720;
			spring = tempSpring;
			addChild(spring);
		}
		
		public function makeSpinner(){
			var tempSpinner:Spinner = new Spinner("SpinGon", 60, 4, 0x000000, 3, false, 10);
			tempSpinner.x = 500;
			tempSpinner.y = 100;
			tempSpinner.createLinesArray();
			
			spinner = tempSpinner;
			addChild(spinner);
		}
		
		// creates the collision objects by concatting the arrays together
		public function populateCollisionObjects(){
			collisionObjects = collisionObjects.concat(bumpers);
			collisionObjects = collisionObjects.concat(walls);
			collisionObjects.push(leftFlipper, rightFlipper, spring, spinner);
		}
		
		// creates the games quadtree
		public function makeQuadTree(){
			boardTree = new QuadTree(250, 0, 500, 720);
			for each(var go:GameObject in collisionObjects){
				for each(var line:Line in go.shape.linesArray){
					boardTree.addObject(line);
				}
			}
		}
		
		// main update method for game
		public function updateGame(){
			var currentTime = getTimer();  // gets current time for gettimer
			var timeChange = currentTime - previousTime;
			totalTime += currentTime;
			ball.update(collisionObjects, spring, timeChange, boardTree);
			previousTime = currentTime;
			
			leftFlipper.rotateFlipperBack(LEFT_FLIPPER_RETURN);
			rightFlipper.rotateFlipperBack(RIGHT_FLIPPER_RETURN);
			spinner.rotateSpinner(timeChange);
			
			// updates every so often
			if((int)(totalTime % 1) == 0){
				makeQuadTree();
			}
			for each(var go:GameObject in collisionObjects){
				go.updateObject(timeChange);
			}
		}
		
	}
	
}
